<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>用户管理</title>
	<%@ include file="/WEB-INF/pages/common/frameCommonJs.jsp"  %>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/commonService.js'></script>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/userService.js'></script>
	<script>
		$(function(){
			   // 店铺员工表格
			   $('#userList').datagrid({
				iconCls:'icon-save',
				nowrap: false,
				striped: true,
				collapsible:true,
				url:'<%=request.getContextPath()%>/main!usersOfStore.action',
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'code',
				singleSelect:true,
				columns:[[
					{field:'loginName',title:'登录名',width:120},
					{field:'isAdmin',title:'角色',width:120,
						formatter:function(value,row,index){
		                    if(row.isAdmin=="1"){
								return "<font color='blue'>店长</font>";
							}
							else if(row.isAdmin=="2"){
								return "<font color='red'>管理员</font>";
							}else {
								return "员工";
							}
						}
	                },
					{field:'phone',title:'联系电话',width:120},
					{field:'address',title:'住址',width:120},
					{field:'storeName',title:'所属店铺',width:120}
				]],
				pageSize:15,  
                pageList:[5,10,15,20],  
                fitColumns: true,  
				pagination:true,
				rownumbers:true
			});
			   
		   $('a[name=query]').click(function(){
				var loginName = $('#loginName').val();
				$('#userList').datagrid('reload',{loginName:loginName});
			});
		
			$('#loginName').keydown(function(e){
		      if(e.keyCode==13) {
		    	  var loginName = $('#loginName').val();
		    	  $('#userList').datagrid('reload',{loginName:loginName});
		      }
		    });
			
		}); 
		var saveOrUpdateUrl = "<%=request.getContextPath()%>/main!saveUser.action";
		function saveUser(){  
			if($("#user\\.id").val()!="" && $("#user\\.id").val() != null){
				saveOrUpdateUrl = "<%=request.getContextPath()%>/main!updateUser.action";
			}
		    $('#fm').form('submit',{  
		        url:saveOrUpdateUrl,  
		        onSubmit: function(){  
		        	var pwd = $("#user\\.password").val();
					var rePwd = $("#rePassword").val();
		        	if(pwd != rePwd){
		        		$.messager.alert("提示信息","输入的两次密码不一致!");
		        		$("#user\\.password").val('');
		        		$("#rePassword").val('');
		        		return false;
		        	}
		            return $(this).form('validate');  
		        },  
		        success: function(result){  
		            var result = eval('('+result+')');
		            var msg = result.name;
		            if($("#user\\.id").val()!=""){
		            	msg = msg + "修改成功";
		            }else{
		            	msg = msg + "新增成功";
		            }
		            if (result!=''){
		            	$.messager.show({title:"提示信息:",msg:msg,showType:'slide'});
		                $('#dlg').dialog('close');      // close the dialog  
		                $('#userList').datagrid('reload');    // reload the user data  
		            } else {  
		                $.messager.show({  
		                    title: '新增成功',  
		                    msg: result.msg  
		                });  
		            }  
		        }  
		    });  
		}
		
		function toEditUser(){
			$("#user\\.loginName").attr('disabled','disabled');
			saveOrUpdateUrl = "<%=request.getContextPath()%>/main!updateUser.action";
			var node=$("#userList").datagrid("getSelected");
			if(node == null){
				$.messager.alert("提示信息:","请选择用户!");
				return;
			}
			$('#dlg').dialog('open').dialog('setTitle','修改用户');
			$("#user\\.id").val(node.id);
			$("#user\\.name").val(node.name);
			$("#user\\.loginName").val(node.loginName);
			$("#user\\.password").val(node.password);
			$("#rePassword").val(node.rePassword);
			$("#user\\.phone").val(node.phone);
			$("#user\\.address").val(node.address);
			$("#user\\.note").val(node.note);
		}
		
		function toAddUser(){
			$('#dlg').dialog('open').dialog('setTitle','新增用户');
			$("#user\\.loginName").removeAttr('disabled');
			$("#user\\.id").val('');
			saveOrUpdateUrl = "<%=request.getContextPath()%>/main!saveUser.action";
			$("#user\\.loginName").blur(function(){
				var loginName = $("#user\\.loginName").val();
				userService.isLoginNameUsed(loginName,function(data){
					if(data == true){
						$.messager.alert("提示信息","【" + loginName + "】用户名已经被占用！",'warning');
						$("#user\\.loginName").val("");
						$("#user\\.loginName").focus();
					}else{
						$('#loginNameSpan').html("");
					}
				});
			});
			$('#dlg').form('clear');
			
		}
		
		function toDelUser(){
			var node = $('#userList').datagrid('getSelected');
			var condt = new delOrgProWhCondtn();
   			condt.uid = node.id;
   			if(node.isAdmin > 0) {
   				$.messager.alert("提示信息","店长不能删除!",'warning');
				return;
   			}
			commonService.valDelOrgProWH(condt,function(data){
				if(data=="no"){
					$.messager.alert("提示信息","该用户已在业务单据中使用，不能删除!",'warning');
					return;
				}
				$.messager.confirm('删除用户信息', '确认删除[' + node.name + ']吗?', function(r){
					if(r){
						$.ajax({
							type:"post",
							data:"user.id=" + node.id,
							url:"<%=request.getContextPath()%>/main!deleteUser.action",
							success:function(result){
								var data = eval('(' + result + ')');
								$.messager.show({title:"提示信息",msg:"删除成功",showType:'slide'});
								$('#userList').datagrid('reload');
							}
						});
						
					}
				});
			});
		}
		
		
		function clearData(){
			$('#loginName').val('');
		}
		
	</script>
</head>
<body class="easyui-layout" >
	<div region="center" title="员工信息管理" style="overflow:hidden;">
	<div id="tb" class="datagrid-toolbar">  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:toAddUser()">新增</a>  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="javascript:toEditUser();">修改</a>  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:toDelUser()">删除</a>  
	</div>
	<table style="font-size:12px; background:#fafafa; padding:10px;" border="0">
		<tr>
			<td>登录名：
				<input type="text" id="loginName" name="loginName" class="easyui-validatebox"  value="${loginName}" />
			</td>
			<td>
				<a href="#" class="easyui-linkbutton" name="query">查询</a>
 				<a href="#" class="easyui-linkbutton" onclick="clearData()">重置</a>
			</td>
		</tr>
	</table>

	<br />
	<table id="userList"></table>
	</div>
	
	<div id="dlg" class="easyui-dialog" style="width:400px;resizable="true"
			padding:10px 20px" closed="true" buttons="#dlg-buttons">  
    <form id="fm" method="post">
    <center>
    <table border="0">
    	<input id="user.id" name="user.id" type="hidden" />  
        	<tr>
        		<td align="right">用户名：</td>
        		 <td align="left">
        		 	<input id="user.loginName" name="user.loginName" class="easyui-validatebox" required="true" />
        		 	<font color="red">*</font><br/>
        		 	<span id="loginNameSpan"></span>
        			<span id="passwordSpan"></span>
        		 </td>  
        	</tr>
        	<tr>
        		<td align="right">密码：</td>
            	<td align="left">
            		<input type="password" id="user.password" name="user.password" class="easyui-validatebox" required="true" onclick="this.value=''" />
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
        		<td align="right">重复密码：</td>
            	<td align="left">
            		<input type="password" id="rePassword" name="rePassword" class="easyui-validatebox" required="true" onclick="this.value=''" />
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
            	<td align="right">姓名：</td>
            	<td align="left">
            		<input id="user.name" name="user.name" class="easyui-validatebox" required="true" />
            		<font color="red">*</font>  
            	</td>
            </tr>
            <tr>
        		<td align="right">联系电话:</td>
            	<td align="left">
            		<input id="user.phone" name="user.phone" required="true"/>
            	</td>
            </tr>
            <tr>
        		<td align="right">住址:</td>
            	<td align="left">
            		<input id="user.address" name="user.address"  />
            	</td>
            </tr>
            <tr>
            	<td align="right">描述:</td>
            	<td align="left">
           			<textarea id="user.note" name="user.note" class="easyui-validatebox" rows="4" cols="25" maxlength="200"></textarea>
           		</td>
           	</tr>
        </table>
        </center>
        </form> 
	</div>
	<div id="dlg-buttons">  
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser();">保存</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>  
</div> 
</body>
</html>
